﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DALFactory;
using KPIS.DBM;


namespace KPIS.GERP.GHRMS.BLL
{
    public class CostLivingsBLL : ICostLivingsBLL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        public DataSet ListCostLivingsFilter()
        {
            //dbManager.CreateParameters(1);
            //dbManager.AddParameters(0, "@RecordStatus", SystemInfo._ActiveRecordStatus);

            DataSet ds = new DataSet();
            dbManager.Open();
            ds = dbManager.ExecuteDataSet(CommandType.Text, " select * from hrm_pr_cost_livings where  start_date =(  select max(b.start_date)  from hrm_pr_cost_livings as a INNER JOIN ( select  *  from hrm_pr_cost_livings where start_date < getdate()  and record_status = 'U') as b on  a.cost_seq=b.cost_seq) and record_status = 'U'");
            dbManager.Dispose();
            return ds;
        }

        public DataSet ListCostLivingsAll()
        {
            //dbManager.CreateParameters(1);
            //dbManager.AddParameters(0, "@RecordStatus", SystemInfo._ActiveRecordStatus);

            DataSet ds = new DataSet();
            dbManager.Open();
            ds = dbManager.ExecuteDataSet(CommandType.Text, "select *  from hrm_pr_cost_livings where record_status not like 'D'");
            dbManager.Dispose();
            return ds;
        }


        public void Insert(string cost_ref_no, DateTime start_date, Nullable<DateTime> end_date, Double cost_min, Double cost_max, Double cost_add, string remarks, string record_status, string CreatedBy)
        {
            try
            {
                dbManager.CreateParameters(10);
                dbManager.AddParameters(0, "@cost_ref_no", cost_ref_no);
                dbManager.AddParameters(1, "@start_date", start_date);
                dbManager.AddParameters(2, "@end_date", end_date);
                dbManager.AddParameters(3, "@cost_min", cost_min);
                dbManager.AddParameters(4, "@cost_max", cost_max);
                dbManager.AddParameters(5, "@cost_add", cost_add);
                dbManager.AddParameters(6, "@remarks", remarks);
                dbManager.AddParameters(7, "@RecordStatus", record_status);
                dbManager.AddParameters(8, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(9, "@CreatedWhen", DateTime.Today);

                dbManager.Open();
                dbManager.ExecuteNonQuery(CommandType.Text, "INSERT INTO hrm_pr_cost_livings (cost_ref_no, start_date, end_date, cost_min, cost_max, cost_add, remarks, RECORD_STATUS,created_by,created_when) VALUES (@cost_ref_no, @start_date, @end_date, @cost_min, @cost_max, @cost_add, @remarks, @RecordStatus, @CreatedBy, @CreatedWhen)");
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }

        public void Update(int Sequence, Double cost_min, Double cost_max, Double cost_add, string remarks, string record_status, string UpdateBy)
        {
            try
            {
                dbManager.CreateParameters(8);
                dbManager.AddParameters(0, "@sequence", Sequence);
                dbManager.AddParameters(1, "@cost_min", cost_min);
                dbManager.AddParameters(2, "@cost_max", cost_max);
                dbManager.AddParameters(3, "@cost_add", cost_add);
                dbManager.AddParameters(4, "@remarks", remarks);
                dbManager.AddParameters(5, "@RecordStatus", record_status);
                dbManager.AddParameters(6, "@updated_by", UpdateBy);
                dbManager.AddParameters(7, "@updated_when", DateTime.Today);
                dbManager.Open();

                dbManager.ExecuteNonQuery(CommandType.Text, "UPDATE hrm_pr_cost_livings SET cost_min=@cost_min, cost_max=@cost_max, cost_add=@cost_add, remarks=@remarks, record_status=@RecordStatus,updated_by=@updated_by,updated_when=@updated_when WHERE cost_seq=@sequence");
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }
        public void Delete(int Sequence, string UpdateBy)
        {
            try
            {
                dbManager.CreateParameters(4);
                dbManager.AddParameters(0, "@sequence", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdateBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);
                dbManager.Open();
                dbManager.ExecuteNonQuery(CommandType.Text, "UPDATE hrm_pr_cost_livings SET RECORD_STATUS=@record_status,updated_by=@updated_by, updated_when=@updated_when WHERE cost_seq=@sequence");
                dbManager.Dispose();
            }
            catch
            {
                throw;
            }
        }
        
    }
}
